package dao

import (
	"daily-blog/global"
	"daily-blog/model"
)

func CreateComment(comment *model.Comment) error {
	return global.GVA_DB.Create(comment).Error
}

func DeleteComment(postId, commentId string) error {
	return global.GVA_DB.Delete(&model.Comment{}, " post_id=? and comment_id=?", postId, commentId).Error
}

func GetCommentListByPostId(postId string) (comments []model.CommentInfo, err error) {
	global.GVA_DB.Model(&model.Comment{}).Select("comment_id", "author", "content", "post_id", "email").Find(&comments, "post_id = ?", postId)
	return
}
func GetCommentList() ([]*model.PostComment, error) {
	var commentList []*model.PostComment
	err := global.GVA_DB.Model(&model.Post{}).Select("posts.post_id ", " title ", " comment_id ", " c.author ", "c.content ", "c.email ").
		Joins("left join comments c on  posts.post_id = c.post_id and c.deleted_at is null ").Order("post_id desc").Find(&commentList).Error
	return commentList, err
}
